<?php

namespace App\Services;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Pagination\LengthAwarePaginator;

class AnonymousResourceCollection extends ResourceCollection
{
    /**
     * The name of the resource being collected.
     *
     * @var string
     */
    public $collects;

    /**
     * Indicates if the collection keys should be preserved.
     *
     * @var bool
     */
    public $preserveKeys = false;

    /**
     * Create a new anonymous resource collection.
     *
     * @param  mixed  $resource
     * @param  string  $collects
     * @return void
     */
    public function __construct($resource, $collects)
    {
        $this->collects = $collects;

        parent::__construct($resource);
    }


    /**
     * @param Request $request
     * @return array<mixed>|Arrayable|\JsonSerializable
     */
    public function toArray(Request $request): array|\JsonSerializable|Arrayable
    {

        if ($this->resource instanceof LengthAwarePaginator) {

            $paginated = $this->resource;
            return [
                'lists' => $this->collection->toArray(),
                'pageNo' => $paginated->currentPage(),
                'pageSize' => $paginated->perPage(),
                'count' => $paginated->total(),
            ];
        } else {
            return $this->collection->toArray();
        }
    }
}
